// Integration test for Python-style visibility in module system

use nrc::ast::ImportDecl;
use nrc::error::Location;
use nrc::semantic::module_resolver::ModuleResolver;
use std::fs;
use std::path::PathBuf;

#[test]
fn test_module_visibility_integration() {
    // Create a temporary test module
    let temp_file = "/tmp/test_visibility_module.nr";
    let content = r#"func public_add() int32 {
    return 100
}

func public_subtract() int32 {
    return 50
}

func _private_multiply() int32 {
    return 200
}

func _private_divide() int32 {
    return 25
}
"#;

    fs::write(temp_file, content).expect("Failed to write test module");

    // Create module resolver and resolve the import
    let mut resolver =
        ModuleResolver::new(PathBuf::from("/tmp")).expect("Failed to create module resolver");
    let import = ImportDecl {
        path: "./test_visibility_module.nr".to_string(),
        default_import: None,
        named_imports: vec![],
        namespace_import: None,
        location: Location::new(1, 1, 0),
    };

    resolver
        .resolve_import(&import)
        .expect("Failed to resolve import");

    // Get the loaded module - need to use the resolved path
    let resolved_path = resolver
        .resolve_module_path("./test_visibility_module.nr")
        .expect("Failed to resolve path");
    let module = resolver
        .get_module(&resolved_path)
        .expect("Module should be loaded");

    // Verify public functions are exported
    assert!(
        module.exports.contains_key("public_add"),
        "public_add should be exported"
    );
    assert!(
        module.exports.contains_key("public_subtract"),
        "public_subtract should be exported"
    );

    // Verify private functions are NOT exported
    assert!(
        !module.exports.contains_key("_private_multiply"),
        "_private_multiply should NOT be exported"
    );
    assert!(
        !module.exports.contains_key("_private_divide"),
        "_private_divide should NOT be exported"
    );

    // Verify the correct number of exports (2 public functions)
    assert_eq!(
        module.exports.len(),
        2,
        "Should have exactly 2 exported items"
    );

    // Clean up
    let _ = fs::remove_file(temp_file);
}
